﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dao
{
    public class ZL_WL_Dao
    {
        /// <summary>
        /// 新增一条记录
        /// </summary>
        /// <param name="entity">记录实体</param>
        /// <returns>操作影响的行数</returns>
        public int Add(ZL_WL entity)
        {
            using (var context = new Dao.ErpEntities())
            {
                context.ZL_WLSet.AddObject(entity);
                return context.SaveChanges();
            }
        }

        /// <summary>
        /// 更新一条记录
        /// </summary>
        /// <param name="entity">记录实体</param>
        /// <returns>操作影响的行数</returns>
        public int Update(ZL_WL entity)
        {
            using (var context = new Dao.ErpEntities())
            {
                ZL_WL entity2 = context.ZL_WLSet.FirstOrDefault(c => c.ID == entity.ID);
                if (entity2 != null)
                {
                    entity2 = entity;
                    context.ZL_WLSet.ApplyCurrentValues(entity);
                    return context.SaveChanges();
                }
                return 0;
            }
        }

        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="id">记录Id</param>
        /// <returns>操作影响的行数</returns>
        public int Delete(string id)
        {
            using (var context = new Dao.ErpEntities())
            {
                Guid guid = Guid.Parse(id);
                ZL_WL entity = context.ZL_WLSet.FirstOrDefault(c => c.ID == guid);
                if (entity != null)
                {
                    context.DeleteObject(entity);
                    return context.SaveChanges();
                }
                return 0;
            }
        }

        /// <summary>
        /// 查询一条记录
        /// </summary>
        /// <param name="id">记录Id</param>
        /// <returns>查询结果实体</returns>
        public ZL_WL Query(string id)
        {
            using (var context = new Dao.ErpEntities())
            {
                Guid guid = Guid.Parse(id);
                ZL_WL entity = context.ZL_WLSet.FirstOrDefault(c => c.ID == guid);
                return entity;
            }
        }

        public bool IsExistBH(string bh)
        {
            using (var context = new Dao.ErpEntities())
            {
                ZL_WL entity = context.ZL_WLSet.FirstOrDefault(c => c.BH == bh);
                return entity != null;
            }
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <returns>查询结果集</returns>
        public List<ZL_WL> QueryAll()
        {
            using (var context = new Dao.ErpEntities())
            {
                var q = from c in context.ZL_WLSet
                        select c;

                List<ZL_WL> list = new List<ZL_WL>();
                if (q.Count() > 0)
                {
                    foreach (var c in q)
                    {
                        list.Add(c);
                    }
                }
                return list;
            }
        }

        public List<ZL_WL> QueryBySql(string bh, string mc, string lxbh, string gysId)
        {
            List<ZL_WL> list = new List<ZL_WL>();
            using (var context = new Dao.ErpEntities())
            {
                var q = from c in context.ZL_WLSet
                        where c.BH.Contains(bh)
                        select c;

                if (!string.IsNullOrEmpty(mc))
                {
                    q = q.Where(c => c.MC.Contains(mc));
                }

                if (!string.IsNullOrEmpty(lxbh) && (!"0".Equals(lxbh)))
                {
                    q = q.Where(c => c.LXBH == lxbh);
                }

                if (!string.IsNullOrEmpty(gysId) && (!"0".Equals(gysId)))
                {
                    Guid gid = Guid.Parse(gysId);
                    q = q.Where(c => c.GYSID == gid);
                }

                foreach (ZL_WL c in q)
                {
                    list.Add(c);
                }

                return list;
            }
        }
    }
}
